package com.example.zhihuijiaju;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.io.IOException;

import okhttp3.Response;

import static android.widget.Toast.LENGTH_SHORT;

public class Login extends AppCompatActivity {
    private static final String TAG = "Login";
    private static String Token="";

    public static final  int requestCode = 2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
    }



    public void onClick(View view) {
        Log.i(TAG,"返回");
        switch (view.getId())
        {
            case R.id.fanhui:Log.i(TAG,"返回");fanhui();break;
            case R.id.logining:Log.i(TAG,"登录");loging();break;
            case R.id.zhuce:Toast.makeText(Login.this,"暂未开放",LENGTH_SHORT).show();break;
        }
    }

    private void fanhui() {
        //文件读取
        Token= FileOperations.file_read(this,"ToKen");
        if(Token.length()<10) {
            Intent home = new Intent(Intent.ACTION_MAIN);
            home.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            home.addCategory(Intent.CATEGORY_HOME);
            startActivity(home);
        }
        else {
            this.finish();
            overridePendingTransition(R.anim.bottomin_enter,R.anim.bottomout_exit);
        }

    }

    //设置返回按钮：不应该退出程序---而是返回桌面
    //复写onKeyDown事件
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            fanhui();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    public  void loging()
    {
        EditText domainname = findViewById(R.id.domainname);
        EditText username = findViewById(R.id.username);
        EditText password = findViewById(R.id.password);

        ProgressDialog progressDialog = ProgressDialog.show(this,"","正在登录");
        //得到凭证回调
        HttpLink httpLink = new HttpLink("",
                "https://iam.cn-north-4.myhuaweicloud.com/v3/auth/tokens ",
                "{'auth': {'identity': {'methods': ['password'  ]," +
                        "'password': { 'user': { 'name': '"+username.getText().toString()+"','password': '"+password.getText().toString()+"', 'domain': {'name':'"+domainname.getText().toString()+"' }  } }}, " +
                        "'scope': { 'project': {   'name': 'cn-north-4'   }   }   } }",
                Method.POST);
        httpLink.HttpListener(new HttpChangeListener() {
            @Override
            public void HttpChange(Response response) {
                progressDialog.dismiss();
                String res = null;
                try {
                    res = response.body().string();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Log.i(TAG,res);
                try{
                    Token = response.header("X-Subject-Token");
                    if(Token!="") {
                        Log.i(TAG,"登录成功");
                        FileOperations.file_write(Login.this,"ToKen",Token);
                        Intent resultIntent = new Intent();
                        Bundle bundle = new Bundle();
                        bundle.putString("token", Token);
                        resultIntent.putExtras(bundle);
                        Login.this.setResult(RESULT_OK,resultIntent);
                        Login.this.finish();
                        overridePendingTransition(R.anim.bottomin_enter,R.anim.bottomout_exit);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(Login.this,"登录失败",LENGTH_SHORT).show();
                }

            }
        });
    }
}
